//
//  UIView+Bezier.m
//  HSYCustomNavigationController
//
//  Created by key on 2017/2/10.
//  Copyright © 2017年 Scho.CustomNavigationController.Demo. All rights reserved.
//

#import "UIView+Bezier.h"

@implementation UIView (Bezier)

#pragma mark - Draw Rectangle

- (void)bezierForRectangleByRect:(CGRect)rect rectangleFillColor:(UIColor *)fillColor rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:rect];
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = bezierPath.CGPath;
    layer.fillColor = fillColor.CGColor;
    layer.strokeColor = strokeColor.CGColor;
    layer.lineWidth = lineWidth;
    
    [self.layer addSublayer:layer];
}

- (void)bezierForRectangleByRect:(CGRect)rect rectangleColor:(UIColor *)color
{
    [self bezierForRectangleByRect:rect rectangleFillColor:color rectangleStrokeColor:color rectangleLineWidth:1.0f];
}

- (void)bezierForRectangleByRect:(CGRect)rect rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    [self bezierForRectangleByRect:rect rectangleFillColor:[UIColor clearColor] rectangleStrokeColor:strokeColor rectangleLineWidth:lineWidth];
}

#pragma mark - Draw Rounded Rectangle

- (void)bezierForRectangleByRoundedRect:(CGRect)rect cornerRadius:(CGFloat)radius rectangleFillColor:(UIColor *)fillColor rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = bezierPath.CGPath;
    layer.fillColor = fillColor.CGColor;
    layer.strokeColor = strokeColor.CGColor;
    layer.lineWidth = lineWidth;
    
    [self.layer addSublayer:layer];
}

- (void)bezierForRectangleByRoundedRect:(CGRect)rect cornerRadius:(CGFloat)radius rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    [self bezierForRectangleByRoundedRect:rect cornerRadius:radius rectangleFillColor:[UIColor clearColor] rectangleStrokeColor:strokeColor rectangleLineWidth:lineWidth];
}

- (void)bezierForRectangleByRoundedRect:(CGRect)rect cornerRadius:(CGFloat)radius rectangleColor:(UIColor *)color
{
    [self bezierForRectangleByRoundedRect:rect cornerRadius:radius rectangleFillColor:color rectangleStrokeColor:color rectangleLineWidth:1.0f];
}

#pragma mark - Draw Random Rounded Rectangle

- (void)bezierForRectangleByRoundedRect:(CGRect)rect roundingCorners:(UIRectCorner)corner cornerRadii:(CGSize)radii rectangleFillColor:(UIColor *)fillColor rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radii];
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = bezierPath.CGPath;
    layer.fillColor = fillColor.CGColor;
    layer.strokeColor = strokeColor.CGColor;
    layer.lineWidth = lineWidth;
    
    [self.layer addSublayer:layer];
}

- (void)bezierForRectangleByRoundedRect:(CGRect)rect roundingCorners:(UIRectCorner)corner cornerRadius:(CGFloat)radius rectangleFillColor:(UIColor *)fillColor rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    [self bezierForRectangleByRoundedRect:rect roundingCorners:corner cornerRadii:CGSizeMake(radius, radius) rectangleFillColor:fillColor rectangleStrokeColor:strokeColor rectangleLineWidth:lineWidth];
}

- (void)bezierForRectangleByRoundedRect:(CGRect)rect roundingCorners:(UIRectCorner)corner cornerRadius:(CGFloat)radius rectangleColor:(UIColor *)color
{
    [self bezierForRectangleByRoundedRect:rect roundingCorners:corner cornerRadii:CGSizeMake(radius, radius) rectangleFillColor:color rectangleStrokeColor:color rectangleLineWidth:1.0f];
}

- (void)bezierForRectangleByRoundedRect:(CGRect)rect roundingCorners:(UIRectCorner)corner cornerRadius:(CGFloat)radius rectangleStrokeColor:(UIColor *)strokeColor rectangleLineWidth:(CGFloat)lineWidth
{
    [self bezierForRectangleByRoundedRect:rect roundingCorners:corner cornerRadii:CGSizeMake(radius, radius) rectangleFillColor:[UIColor clearColor] rectangleStrokeColor:strokeColor rectangleLineWidth:lineWidth];
}

#pragma mark - Draw Round

- (void)bezierForOvalInRect:(CGRect)rect roundFillColor:(UIColor *)fillColor roundStrokeColor:(UIColor *)strokeColor roundLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = bezierPath.CGPath;
    layer.fillColor = fillColor.CGColor;
    layer.strokeColor = strokeColor.CGColor;
    layer.lineWidth = lineWidth;
    
    [self.layer addSublayer:layer];
}

- (void)bezierForOvalInRect:(CGRect)rect roundStrokeColor:(UIColor *)strokeColor roundLineWidth:(CGFloat)lineWidth
{
    [self bezierForOvalInRect:rect roundFillColor:[UIColor clearColor] roundStrokeColor:strokeColor roundLineWidth:lineWidth];
}

- (void)bezierForOvalInRect:(CGRect)rect roundColor:(UIColor *)color
{
    [self bezierForOvalInRect:rect roundFillColor:color roundStrokeColor:color roundLineWidth:1.0f];
}

#pragma mark - Draw Ellipse

- (void)bezierForEllipseByRect:(CGRect)rect ellipseFillColor:(UIColor *)fillColor ellipseStrokeColor:(UIColor *)strokeColor ellipseLineWidth:(CGFloat)lineWidth
{
    NSParameterAssert(CGRectGetWidth(rect) == CGRectGetHeight(rect));//椭圆形状高度和宽度不能相等，否则相当于一个圆形
    [self bezierForOvalInRect:rect roundFillColor:fillColor roundStrokeColor:strokeColor roundLineWidth:lineWidth];
}

- (void)bezierForEllipseByRect:(CGRect)rect ellipseStrokeColor:(UIColor *)strokeColor ellipseLineWidth:(CGFloat)lineWidth
{
    [self bezierForEllipseByRect:rect ellipseFillColor:[UIColor clearColor] ellipseStrokeColor:strokeColor ellipseLineWidth:lineWidth];
}

- (void)bezierForEllipseByRect:(CGRect)rect ellipseColor:(UIColor *)color
{
    [self bezierForEllipseByRect:rect ellipseFillColor:color ellipseStrokeColor:color ellipseLineWidth:1.0f];
}

#pragma mark - Draw Arc

- (void)bezierForArcCenter:(CGPoint)center arcRadius:(CGFloat)radius arcStartAngle:(CGFloat)startAngle arcEndAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise arcFillColor:(UIColor *)fillColor arcLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise];
    CAShapeLayer *layer = [CAShapeLayer layer];
    
    layer.path = bezierPath.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = fillColor.CGColor;
    layer.lineWidth = lineWidth;
    
    [self.layer addSublayer:layer];
}

- (void)bezierClockwiseForArcCenter:(CGPoint)center arcRadius:(CGFloat)radius arcStartAngle:(CGFloat)startAngle arcEndAngle:(CGFloat)endAngle arcFillColor:(UIColor *)fillColor arcLineWidth:(CGFloat)lineWidth
{
    [self bezierForArcCenter:center arcRadius:radius arcStartAngle:startAngle arcEndAngle:endAngle clockwise:YES arcFillColor:fillColor arcLineWidth:lineWidth];
}

- (void)bezierCounterClockwiseForArcCenter:(CGPoint)center arcRadius:(CGFloat)radius arcStartAngle:(CGFloat)startAngle arcEndAngle:(CGFloat)endAngle arcFillColor:(UIColor *)fillColor arcLineWidth:(CGFloat)lineWidth
{
    [self bezierForArcCenter:center arcRadius:radius arcStartAngle:startAngle arcEndAngle:endAngle clockwise:YES arcFillColor:fillColor arcLineWidth:lineWidth];
}

#pragma mark - Draw Curves

- (void)bezierForCurvesByStartPoint:(CGPoint)sPoint endPoint:(CGPoint)ePoint controlPoint:(CGPoint)cPoint curvesStrokeColor:(UIColor *)strokeColor curvesLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [self shapeLayerByStrokeColor:strokeColor bezierPath:bezierPath lineWidth:lineWidth curveCompleted:^{
        
        [bezierPath moveToPoint:sPoint];
        [bezierPath addQuadCurveToPoint:ePoint controlPoint:cPoint];
    }];
    [self.layer addSublayer:layer];
}

- (void)bezierForCurvesByStartPoint:(CGPoint)sPoint endPoint:(CGPoint)ePoint controlPoint1:(CGPoint)cPoint1 controlPoint2:(CGPoint)cPoint2 curvesStrokeColor:(UIColor *)strokeColor curvesLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [self shapeLayerByStrokeColor:strokeColor bezierPath:bezierPath lineWidth:lineWidth curveCompleted:^{
        
        [bezierPath moveToPoint:sPoint];
        [bezierPath addCurveToPoint:ePoint controlPoint1:cPoint1 controlPoint2:cPoint2];
    }];
    [self.layer addSublayer:layer];
}

- (CAShapeLayer *)shapeLayerByStrokeColor:(UIColor *)strokeColor bezierPath:(UIBezierPath *)path lineWidth:(CGFloat)lineWidth curveCompleted:(void(^)())completed
{
    return [self shapeLayerByStrokeColor:strokeColor fillColor:[UIColor clearColor] bezierPath:path lineWidth:lineWidth curveCompleted:^{
        if (completed) {
            completed();
        }
    }];
}

- (CAShapeLayer *)shapeLayerByStrokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor bezierPath:(UIBezierPath *)path lineWidth:(CGFloat)lineWidth curveCompleted:(void(^)())completed
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = fillColor.CGColor;
    layer.strokeColor = strokeColor.CGColor;
    layer.lineWidth = lineWidth;
    
    if (completed) {
        completed();
    }
    
    layer.path = path.CGPath;
    return layer;
}

#pragma mark - Draw Triangle

- (void)bezierForTriangleByStartPoint:(CGPoint)sPoint leftPoint:(CGPoint)lPoint rightPoint:(CGPoint)rPoint triangleFillColor:(UIColor *)fillColor
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [self shapeLayerByStrokeColor:fillColor fillColor:fillColor bezierPath:bezierPath lineWidth:0.01 curveCompleted:^{
        
        [bezierPath moveToPoint:sPoint];
        [bezierPath addLineToPoint:lPoint];
        [bezierPath addLineToPoint:rPoint];
        [bezierPath addLineToPoint:sPoint];
    }];
    [self.layer addSublayer:layer];
}

- (void)bezierForTriangleByStartPoint:(CGPoint)sPoint leftPoint:(CGPoint)lPoint rightPoint:(CGPoint)rPoint triangleStrokeColor:(UIColor *)strokeColor triangleLineWidth:(CGFloat)lineWidth
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [self shapeLayerByStrokeColor:strokeColor fillColor:[UIColor clearColor] bezierPath:bezierPath lineWidth:lineWidth curveCompleted:^{
        
        [bezierPath moveToPoint:sPoint];
        [bezierPath addLineToPoint:lPoint];
        [bezierPath addLineToPoint:rPoint];
        [bezierPath addLineToPoint:sPoint];
    }];
    [self.layer addSublayer:layer];
}

#pragma mark - Draw Line

- (void)bezierForLineByColor:(UIColor *)color lineWidth:(CGFloat)width startPoint:(CGPoint)sPoint endPoint:(CGPoint)ePoint
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    CAShapeLayer *layer = [self shapeLayerByStrokeColor:color bezierPath:bezierPath lineWidth:width curveCompleted:^{
        
        [bezierPath moveToPoint:sPoint];
        [bezierPath addLineToPoint:ePoint];
    }];
    [self.layer addSublayer:layer];
}




@end
